﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Sh
/// </summary>
/// 
namespace Shut {
	public class BaseUser
	{
		public bool IsSupported = true;
		public string BaseUserGuid;
		public static BaseUser LoadUser(bool CheckValidCookie)
		{
			
			if (CheckValidCookie)
			{
				string buid = GetBrowserCookie(true); // set cookie value on homepage
				// return unsupportd user on homepage anyway
				return new BaseUser
				{
					BaseUserGuid = buid,
					IsSupported = false
				};
			}
			else
			{
				return LoadUser();
			}
		}
		public static BaseUser LoadUser()
		{

			string buid = GetBrowserCookie(false);
			// if buid is nothing, return unsupported user
			// ya3ni buid must be something, this happens when cookies are disabled
			if (string.IsNullOrEmpty(buid))
			{
				return new BaseUser
				{
					BaseUserGuid = Guid.NewGuid().ToString(),
					IsSupported = false
				};
			}

			return new BaseUser
			{
				BaseUserGuid = buid,
				IsSupported = true
			};

		}

		public static string GetBrowserCookie(bool ForceNewCookie)
		{
			HttpCookie _BrowserCookie = HttpContext.Current.Request.Cookies.Get("Sh.BaseUser");
			if (_BrowserCookie == null)
			{
				if (ForceNewCookie)
				{
					Guid id = Guid.NewGuid();
					_BrowserCookie = new HttpCookie("Sh.BaseUser");
					_BrowserCookie.Value = id.ToString();
					_BrowserCookie.Domain = SiteSettings.Instance.Site_Domain;
					_BrowserCookie.Expires = DateTime.Now.AddYears(1);
					HttpContext.Current.Response.Cookies.Add(_BrowserCookie);
					return _BrowserCookie.Value.ToString();
				}
				return string.Empty;
			}
			return _BrowserCookie.Value.ToString();


		}
		// static for now, later make a property in pageuser
		public static string GetUserSiteDirection(bool ForceNewCookie)
		{
			HttpCookie _BrowserCookie = HttpContext.Current.Request.Cookies.Get("Sh.SiteDirection");
			if (_BrowserCookie == null)
			{
				if (ForceNewCookie)
				{
					_BrowserCookie = new HttpCookie("Sh.SiteDirection");
					_BrowserCookie.Value = SiteSettings.Instance.SiteDirection;
					_BrowserCookie.Domain = SiteSettings.Instance.Site_Domain;
					_BrowserCookie.Expires = DateTime.Now.AddYears(1);
					HttpContext.Current.Response.Cookies.Add(_BrowserCookie);
					return _BrowserCookie.Value.ToString();
				}
				return string.Empty;
			}
			return _BrowserCookie.Value.ToString();

		}
		public static void SetUserSiteDirection(string siteDirection)
		{
			SiteSettings.Instance.SiteDirection = siteDirection;

			HttpCookie _BrowserCookie = HttpContext.Current.Request.Cookies.Get("Sh.SiteDirection");
			_BrowserCookie.Value = siteDirection;
			_BrowserCookie.Domain = SiteSettings.Instance.Site_Domain;
			_BrowserCookie.Expires = DateTime.Now.AddYears(1);
			
		}
		public static string GetUserCulture(bool ForceNewCookie)
		{
			HttpCookie _BrowserCookie = HttpContext.Current.Request.Cookies.Get("Sh.Culture");
			if (_BrowserCookie == null)
			{
				if (ForceNewCookie)
				{
					_BrowserCookie = new HttpCookie("Sh.Culture");
					_BrowserCookie.Value = SiteSettings.Instance.Localization_CultureName;
					_BrowserCookie.Domain = SiteSettings.Instance.Site_Domain;
					_BrowserCookie.Expires = DateTime.Now.AddYears(1);
					HttpContext.Current.Response.Cookies.Add(_BrowserCookie);
					return _BrowserCookie.Value.ToString();
				}
				return string.Empty;
			}
			return _BrowserCookie.Value.ToString();

		}
		public static void SetUserCulture(string siteCulture)
		{
			SiteSettings.Instance.Localization_CultureName = siteCulture;

			HttpCookie _BrowserCookie = HttpContext.Current.Request.Cookies.Get("Sh.Culture");
			_BrowserCookie.Value = siteCulture;
			_BrowserCookie.Domain = SiteSettings.Instance.Site_Domain;
			_BrowserCookie.Expires = DateTime.Now.AddYears(1);

		}
	}
}